//
//  UserInfo.m
//  JiuyaoDriver
//
//  Created by 大橙子 on 2018/11/10.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "UserInfo.h"
#define user_rongcloudToken          @"rongcloudToken"
#define user_token          @"token"

#define user_recordModel          @"record_model"
@implementation UserInfo
singleton_implementation(UserInfo);
+ (instancetype)shareInstance {
    static UserInfo *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!instance) {
            instance = [[UserInfo alloc] init];
        }
    });
    return instance;
}
-(void)saveUserInfoToSanbox{
    [DEFAULT setObject:self.token forKey:user_token];
    [DEFAULT setObject:self.rongcloudToken forKey:user_rongcloudToken];
    
    
    NSData *recordData = [NSKeyedArchiver archivedDataWithRootObject:self.record];
    [DEFAULT setObject:recordData forKey:user_recordModel];
    
    [DEFAULT synchronize];
}
-(void)loadUserInfoFromSanbox{
    self.rongcloudToken = [DEFAULT objectForKey:user_rongcloudToken];
    self.token = [DEFAULT objectForKey:user_token];
    
    NSData *recordData = [DEFAULT objectForKey:user_recordModel];
    if (recordData) {
        self.record = [NSKeyedUnarchiver unarchiveObjectWithData:recordData];
    }
    
    [DEFAULT synchronize];
}
-(void)clearUser{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [userDefaults dictionaryRepresentation];
    for (id  key in dic) {
        [userDefaults removeObjectForKey:key];
    }
    self.rongcloudToken = [DEFAULT objectForKey:user_rongcloudToken];
    self.token = [DEFAULT objectForKey:user_token];
    
    DCLog(@"fadfa---%@",self.token);
    self.record = [[Record alloc]init];
    [DEFAULT synchronize];
}
@end
